package org.mspring.mlog.task.ping.task;

import java.util.Map;

import org.apache.log4j.Logger;
import org.mspring.mlog.api.ping.BaiduPing;
import org.mspring.mlog.api.ping.GooglePing;
import org.mspring.mlog.api.ping.IPing;
import org.mspring.mlog.entity.Post;
import org.mspring.mlog.service.OptionService;
import org.mspring.platform.task.AbstractTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 发表文章后Ping
 * 
 * @author Gao Youbo
 * @since 2013年7月25日
 */
@Component
public class PostPingTask extends AbstractTask {
    private static final Logger log = Logger.getLogger(PostPingTask.class);

    @Autowired
    private OptionService optionService;

    @Override
    protected void doTask(Map<Object, Object> paramMap) throws Exception {
        // TODO Auto-generated method stub
        Object object = paramMap.get("post");
        if (object != null) {
            Post post = (Post) object;
            String sitename = optionService.getOption("sitename");
            String siteurl = optionService.getOption("siteurl");
            String newsurl = siteurl = "/u/" + post.getAuthor().getName() + "/post?id=" + post.getId();

            IPing baiduPing = new BaiduPing();
            boolean ret1 = baiduPing.ping(sitename, siteurl, newsurl, "");
            if (!ret1) {
                log.error("Post Ping Baidu 失败");
            }
            else {
                log.debug("Post Ping Baidu 成功");
            }

            IPing googlePing = new GooglePing();
            boolean ret2 = googlePing.ping(sitename, siteurl, newsurl, "");
            if (!ret2) {
                log.error("Post Ping Google 失败");
            }
            else {
                log.debug("Post Ping Goolge 成功");
            }
        }
    }
}
